<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1">
    <meta name="author" content="杨宁争">
    <title></title>
    <style>
        * {
            box-sizing: border-box;
        }

        body {
            height: 100vh;
            margin: 0;
        }

        #overlay {
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            position: absolute;
            top: 0;
            left: 0;
            display: none;
            z-index: 9998;
        }

        .box {
            width: 400px;
            height: 200px;
            border: 2px solid red;
            position: absolute;
            top: calc(50% - 100px);
            left: calc(50% - 200px);
            background-color: greenyellow;
            display: none;
            z-index: 9999;
        }

        .close {
            cursor: pointer;
            color: white;
        }
     button{
         margin: 0 auto;
     }

    </style>
</head>

<body>
    <div class="btn"><button onclick="showClick()">弹出框</button></div>
    <div id="overlay">
        <div class="box"><span class="close" onclick="closeClick()">&times</span></div>
    </div>
</body>

</html>


<script>
    var overlay = document.querySelector('#overlay');
    var box = document.querySelector('.box');

    function showClick() {
        overlay.style.display = 'block';
        box.style.display = 'block'
    }

    function closeClick() {
        overlay.style.display = 'none';
        box.style.display = 'none'
    }
</script>